package ObjectMethodTest;
/*
当一个用户的用户名和家庭住址都相同，表示同一个用户
 */
public class EqualsTest4 {
    public static void main(String[] args){
        Address a = new Address("南昌","光明大道","666号");
        Address b = new Address("南昌","光明大道","666号");
        Address a1 = new Address("南昌","光明大道","665号");
        Address b1 = new Address("南昌","光明大道","664号");
        User u = new User("123",a);
        User v = new User("123",b);
        User u1 = new User("123",a1);
        User v1 = new User("123",b1);
        System.out.println(u.equals(v)); //true
        System.out.println(u1.equals(v1)); //false
    }
}
class User{
    String name;
    Address address;
    public User(){

    }
    public User(String name, Address address){
        this.name = name;
        this.address = address;
    }
    //重写equals方法
    public boolean equals(Object obj){
        if(null == obj || !(obj instanceof User)) return false;
        if(this == obj) return true;
        User u = (User)obj;
        if(this.name.equals(u.name) && this.address.equals(u.address)){
            return true;
        }
        return false;
    }
}
class Address{
    String city;
    String street;
    String zipcode;

    public Address() {

    }

    public Address(String city, String street, String zipcode) {
        this.city = city;
        this.street = street;
        this.zipcode = zipcode;
    }
    //重写equals方法
    public boolean equals(Object obj){
        if(null == obj || !(obj instanceof Address)) return false;
        if(this == obj) return true;
        Address a = (Address)obj;
        if(this.city.equals(a.city) && this.street.equals(a.street) && this.zipcode.equals(a.zipcode)){
            return true;
        }
        return false;
    }
}
